#-*- coding:utf-8 -*-
import json,time,datetime,base64
import httplib,urllib,urllib2,hashlib


#http格式字符
TimesAsHigh                     ='\x1C\x21\x78\x1B\x21\x50'                     #倍高字体
TimesAsHighBackToNormal         ='\x1C\x21\x70\x1B\x21\x40'                     #倍高字体 恢复正常
NormalCenter                    ='\x1B\x61\x31'                                 #正常居中
NormalCenterBackToNormal        ='\x1B\x61\x31'                                 #正常居中 恢复正常 左对齐
TimesAsHighCenter               ='\x1C\x21\x78\x1B\x21\x50\x1B\x61\x31'         #倍高居中
TimesAsHighCenterBackToNormal   ='\x1C\x21\x70\x1B\x21\x40\x1B\x61\x30'         #倍高居中 恢复正常
TimesAsWide                     ='\x1C\x21\x74\x1B\x21\x60'                     #倍宽字体
TimesAsWideBackToNormal         ='\x1C\x21\x70\x1B\x21\x40'                     #倍宽字体 恢复正常  
TimesAsWideCenter               ='\x1C\x21\x74\x1B\x21\x60\x1B\x61\x31'         #倍宽居中
TimesAsWideCenterBackToNormal   ='\x1C\x21\x70\x1B\x21\x40\x1B\x61\x30'         #倍宽居中 恢复正常
Large                           ='\x1C\x21\x7C\x1B\x21\x70'                     #大号字体
LargeBackToNormal               ='\x1C\x21\x70\x1B\x21\x40'                     #大号字体 恢复正常
LargeCenter                     ='\x1C\x21\x7C\x1B\x21\x70\x1B\x61\x31'         #大号居中
LargeCenterBackToNormal         ='\x1C\x21\x70\x1B\x21\x40\x1B\x61\x30'         #大号居中 恢复正常 左对齐

#http://192.168.1.253:8081/apiPrintTicket/?sn=916800416
#打印信息的处理
def YLW_HttpPrinter_80(aStr_msg):
    dict     = YLW_msg_format(aStr_msg)
    content  = YLW_HttpPrinter_80_format(dict)
    return msgDecode(content)

def base64Decode(aData):
    if (not aData or len(aData) <= 0) :
        return None
    tmpData = 4 - len(aData) % 4
    if tmpData :
        aData += b'='* tmpData
    return base64.b64decode(aData)

    
ylw_58_lenth = 32    #小票所占字符
ylw_80_lenth = 32+15 #小票所占字符
first_msg    = 'MESSAGEBEGIN'  #标识 开头(云不需要)
#
def msgDecode(aMsg):
    enStr = aMsg.decode('utf-8').encode('GB18030')
    return enStr
    

#填充空格
def getBlank(int):
    buf=''
    for a in range(int):
        buf=buf+' '
    return buf;
 
def YLW_HttpPrinter_80_format(dict):
    buf = TimesAsHighCenter + '欢迎光临' 
    buf +='\n' + TimesAsHighCenter + str(dict['market_name']) + '\n' + TimesAsHighCenterBackToNormal
    buf += '\n\n单号:' + ' ' + str(dict['saleno'])
    buf += '\n'+'*' * ylw_80_lenth
    buf += '\n市场名称:' + ' ' + str(dict['market_name'])
    buf += '\n水表名称:' + ' ' + str(dict['posid'])
    buf += '\n水表编号:' + ' ' + str(dict['mac'])
    buf += '\n使用卡号:' + ' ' + str(dict['card'])
    buf += '\n卡片类型:' + ' ' + str(dict['card_type'])
    buf += '\n手机号码:' + ' ' + str(dict['phone'])
    buf += '\n客户姓名:' + ' ' + str(dict['username'])
    buf += '\n结束方式:' + ' ' + str(dict['stop_way'])
    
    ltime=time.localtime(int(time.time()))
    timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime)
    #buf += '\n\n时间:'   + getBlank(1) + str(dict['date']) + getBlank(1) + str(dict['time'])
    buf += '\n\n打印时间:'   + ' ' + timeStr
    buf += '\n' + '-' * ylw_80_lenth

    buf += '\n开始吨数:'     + getBlank(1) + str(dict['start_tones'])
    buf += '\n开始时间:'     + getBlank(1) + str(dict['start_time'])
    
    buf += '\n结束表数:'     + getBlank(1) + str(dict['stop_tones'])
    buf += '\n结束时间:'     + getBlank(1) + str(dict['stop_time'])

    buf += '\n使用时间:'     + getBlank(1) + str(dict['seconds'])
    buf += '\n使用吨数:'     + getBlank(1) + str(dict['tones'])
    
    buf += '\n单价(元/分):'  + getBlank(1) + str(dict['p_second'])
    #buf += '\n单价(元/吨):'  + getBlank(ylw_80_lenth-len(str(dict['p_tones']))       - len('数量件键')) + str(dict['p_tones'])
    buf += '\n计价方式:'     + getBlank(1) + str(dict['vMethod'])
    
    buf += '\n' + '-' * ylw_80_lenth + '\n'
    buf += '\n消费前金额(￥):' + getBlank(ylw_80_lenth-len(str(dict['be_money'])) - len('数量每件键')) + str(dict['be_money'])
    buf += '\n消费总金额(￥):' + getBlank(ylw_80_lenth-len(str(dict['money']))    - len('数量每件键')) + str(dict['money'])
    buf += '\n消费后金额(￥):' + getBlank(ylw_80_lenth-len(str(dict['af_money'])) - len('数量每件键')) + str(dict['af_money'])
    buf += '\n\n'+'*' * ylw_80_lenth
    buf += '\n\n'+NormalCenter + str(dict['url'])
    buf += '\n'+NormalCenter+ str(dict['hotline']) +'\n'+'\n'+'\n'
    return first_msg+buf;  

def YLW_msg_format(aStr_msg):
    i = aStr_msg.split("&v;")
    tmpMsg                   = {}
    tmpMsg["saleno"]         = i[0]
    tmpMsg["posid"]          = i[1]
    tmpMsg["p_num"]          = i[2]
    tmpMsg["start_tones"]    = i[3]
    tmpMsg["tones"]          = i[4]
    tmpMsg["seconds"]        = i[5]
    tmpMsg["start_time"]     = i[6]
    tmpMsg["stop_time"]      = i[7]
    tmpMsg["p_second"]       = i[8]
    tmpMsg["p_tones"]        = i[9]
    tmpMsg["pay_type"]       = i[10]
    tmpMsg["username"]       = base64Decode(i[11])
    tmpMsg["stop_way"]       = i[12]
    tmpMsg["card"]           = i[13]
    tmpMsg["su_card"]        = i[14]
    tmpMsg["stop_tones"]     = i[15]
    tmpMsg["money"]          = str('%.2f'%(float(i[16])))
    tmpMsg["be_money"]       = str('%.2f'%(float(i[17])))
    tmpMsg["af_money"]       = str('%.2f'%(float(i[18])))
    tmpMsg["phone"]          = i[19]
    tmpMsg["card_type"]      = i[20]
    tmpMsg["d_name"]         = i[21]
    tmpMsg["mac"]            = i[22]
    tmpMsg["mincharge"]      = i[23]
    tmpMsg["market_name"]    = '广州菜市场'
    tmpMsg["url"]            = 'www.ylflat.com'
    tmpMsg["hotline"]        = '400-888-8888'
    if i[12] == '0' :
        tmpMsg["stop_way"]     = '刷卡停止'
    if i[20] == '1' :
        tmpMsg["card_type"]    = '主卡'
    if i[20] == '0' :
        tmpMsg["card_type"]    = '副卡'
        
    if i[10] == '0' :
        tmpMsg['vMethod'] = "计时"
    if i[10] == '1' :
        tmpMsg['vMethod'] = "计量"
    if i[10] == '2' :
        tmpMsg['vMethod'] = "智能"
    return tmpMsg
